﻿using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IveelySE.Common;
using IveelySE.NLP.Segment;

namespace IveelySE.NLP.Recognition
{
    [Serializable]
    public class Extract
    {

    }

    /// <summary>
    /// 事件提取器
    /// </summary>
    [Serializable]
    public class EventExtract : Extract
    {
        /// <summary>
        /// 事件信息列表
        /// </summary>
        public static Hashtable List = new Hashtable();


        /// <summary>
        /// 执行分析
        /// </summary>
        /// <param name="text">文本信息</param>
        public void Analysis(string reference, string text)
        {
            string[] texts =
                text.Replace("\r", string.Empty)
                    .Replace("\n", string.Empty)
                    .Replace(" ", string.Empty)
                    .Split(new char[] { '.', '。', '!', '；', '；' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string content in texts)
            {
                AnalysisContent(reference, content);
            }

            //contents = new List<string>(texts);

            //Action<ParallelLoopState> processQueue = lps =>
            //                                             {
            //                                                 //如果队列不为空且在迭代范围之内
            //                                                 if (contents.Count > 0)
            //                                                 {

            //                                                     //执行队列任务
            //                                                     AnalysisContent(reference);
            //                                                 }
            //                                                 //反之
            //                                                 else
            //                                                 {
            //                                                     //结束循环执行
            //                                                     lps.Break();
            //                                                 }
            //                                             };


            ////队列的并行执行
            //processQueue.InParallelWhile(contents.Count > 0);
        }

        private void AnalysisContent(string reference, string content)
        {
            if (content.Length < 10)
            {
                return;
            }
            content = content.Replace("&nbsp", string.Empty).Replace("&raquo",string.Empty);
            Information information = new Information();
            string[] words = WordAnalysis.GetInstance()
                                         .GetWordType(content)
                                         .Split(new char[] {'/',' ' }, StringSplitOptions.RemoveEmptyEntries);
            string describution = content;
            string happenTime = string.Empty;
            string happenPlace = string.Empty;
            string relatePeople = string.Empty;
            for (int i = words.Length - 1; i > 0; i--)
            {

                if (words[i].Equals("t"))
                {
                    happenTime += words[i - 1] + " ";
                }
                else if (words[i].Equals("ns"))
                {
                    happenPlace += words[i - 1] + " ";
                }
                else if (words[i].Equals("nr") || words[i].Equals("r") || words[i].Equals("nz") || words[i].Equals("nt"))
                {
                    relatePeople += words[i - 1] + " ";
                }
            }
            information.Init(new string[] { happenTime, happenPlace, relatePeople, describution });
            if (information.IsQualified())
            {
                information.Reference = reference;
                List.Add(information.Id, information);
            }
        }
    }
}
